

import 'package:flutter_wan_android/core/utils/json_util.dart';

class UserEntity {
  bool admin;
  List<String> chapterTops;
  int coinCount;
  List<int> collectIds;
  String email;
  String icon;
  int id;
  String nickname;
  String password;
  String publicName;
  String token;
  String username;

  UserEntity({
    required this.admin,
    required this.chapterTops,
    required this.coinCount,
    required this.collectIds,
    required this.email,
    required this.icon,
    required this.id,
    required this.nickname,
    required this.password,
    required this.publicName,
    required this.token,
    required this.username,
  });

  factory UserEntity.fromJson(Map<String, dynamic> json) => UserEntity(
    admin: json["admin"],
    chapterTops: List<String>.from(json["chapterTops"].map((x) => x)),
    coinCount: json["coinCount"],
    collectIds: List<int>.from(json["collectIds"].map((x) => x)),
    email: json["email"],
    icon: json["icon"],
    id: json["id"],
    nickname: json["nickname"],
    password: json["password"],
    publicName: json["publicName"],
    token: json["token"],
    username: json["username"],
  );

  Map<String, dynamic> toJson() => {
    "admin": admin,
    "chapterTops": List<dynamic>.from(chapterTops.map((x) => x)),
    "coinCount": coinCount,
    "collectIds": List<dynamic>.from(collectIds.map((x) => x)),
    "email": email,
    "icon": icon,
    "id": id,
    "nickname": nickname,
    "password": password,
    "publicName": publicName,
    "token": token,
    "username": username,
  };



  factory UserEntity.fromDb(Map<String, dynamic> map) {
    return UserEntity(
      admin: (map['admin'] ?? 0) == 1,
      chapterTops: JsonUtil.decodeStringList(map['chapterTops']),
      coinCount: map['coinCount'] ?? 0,
      collectIds: JsonUtil.decodeIntList(map['collectIds']),
      email: map['email'] ?? '',
      icon: map['icon'] ?? '',
      id: map['id'],
      nickname: map['nickname'] ?? '',
      password: map['password'] ?? '',
      publicName: map['publicName'] ?? '',
      token: map['token'] ?? '',
      username: map['username'] ?? '',
    );
  }


  Map<String, dynamic> toDb() {
    return {
      'id': id,
      'admin': admin ? 1 : 0,
      'chapterTops': JsonUtil.encodeJson(chapterTops),
      'coinCount': coinCount,
      'collectIds': JsonUtil.encodeJson(collectIds),
      'email': email,
      'icon': icon,
      'nickname': nickname,
      'password': password,
      'publicName': publicName,
      'token': token,
      'username': username,
    };
  }


}